#include <vector>
#include <stack>

using namespace std;

int largestRectangleArea(vector<int>& height) {
    stack<int> pl;
    int i, max_area = 0;
    int n = height.size();
    i = 0;
    while (i <= n) {
        if (i == n || (!pl.empty() && height[pl.top()] > height[i])) {
            if (!pl.empty()) {
                int t = pl.top();
                pl.pop();
                int width = pl.empty() ? i : (i - t);
                if (max_area < width * height[t]) {
                    max_area = width * height[t];
                }
            }
            else {
                break;
            }
        }
        else {
            pl.push(i);
            i++;
        }
    }
    
    return max_area;
}
